package com.caocaog.market.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.caocaog.market.entity.Information;
import com.caocaog.market.service.InformationService;
import com.caocaog.market.vo.InformationVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.aspect.annotation.Trim;
import org.jeecg.common.system.base.controller.GenericController;
import org.jeecg.common.system.domain.QueryRequest;
import org.jeecg.common.system.query.PageGenerator;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

/**
 * 信息发布
 *
 * @author chechen
 * @date 2021-11-13
 */
@Slf4j
@RestController
@Api(value = "information", tags = InformationController.TABLE_MEANING)
@RequestMapping("/information")
public class InformationController extends GenericController<Information, InformationService> {

    public static final String TABLE_MEANING = "信息发布";

    private static final String PAGE = "信息发布-分页列表查询";
    @Trim
    @ApiOperation(value = PAGE, notes = PAGE)
    @GetMapping(value = "/list")
    @RequiresPermissions("information:view")
    public Result<?> queryInformationPage(Information information, QueryRequest queryRequest) {
        Page<InformationVO> page = PageGenerator.createPageSort(queryRequest);
        IPage<InformationVO> pageList = super.service.queryInformationPage(page, information);
        return Result.OK(pageList);
    }

    private static final String GET_BY_ID = "根据id获取信息内容";
    @AutoLog(value = GET_BY_ID)
    @ApiOperation(value = GET_BY_ID, notes = GET_BY_ID)
    @GetMapping(value = "/detail")
    @RequiresPermissions("information:view")
    public Result<?> getDetailById(String id) {
        return Result.OK(super.service.getDetailById(id));
    }

    /*private static final String ADD = "信息发布-添加";
    @AutoLog(value = ADD)
    @ApiOperation(value = ADD, notes = ADD)
    @PostMapping(value = "/add")
    @RequiresPermissions("information:add")
    public Result<?> add(@RequestBody InformationVO informationVO) {
        return Result.OK(ADD_SUCCESS, super.service.addInformation(informationVO));
    }*/

    /**
     * P.S. 状态为 审核中或已完成 时不能改
     */
    /*private static final String EDIT = "信息发布-编辑";
    @AutoLog(value = EDIT)
    @ApiOperation(value = EDIT, notes = EDIT)
    @PutMapping(value = "/edit")
    @RequiresPermissions("information:edit")
    public Result<?> edit(@RequestBody InformationVO informationVO) {
        return Result.OK(EDIT_SUCCESS, super.service.updateInformation(informationVO));
    }*/

    /*private static final String SUBMIT_REVIEW = "信息发布-提交审核";
    @AutoLog(value = SUBMIT_REVIEW)
    @ApiOperation(value = SUBMIT_REVIEW, notes = SUBMIT_REVIEW)
    @PutMapping(value = "/submitReview")
    @RequiresPermissions("information:review")
    public Result<?> submitReview(@RequestParam String id) {
        return Result.OK("提交审核成功", super.service.submitReview(id));
    }*/

    private static final String PUT_ON_SALE = "信息发布-上架";
    @AutoLog(value = PUT_ON_SALE)
    @ApiOperation(value = PUT_ON_SALE, notes = PUT_ON_SALE)
    @PutMapping(value = "/putOnSale")
    public Result<?> putOnSaleInfo(@RequestParam("id") String id) {
        return Result.OK("上架信息发布成功", super.service.putOnSaleInfo(id));
    }

    private static final String PULL_OFF_SHELVES = "信息发布-下架";
    @AutoLog(value = PULL_OFF_SHELVES)
    @ApiOperation(value = PULL_OFF_SHELVES, notes = PULL_OFF_SHELVES)
    @PutMapping(value = "/pullOffShelves")
    public Result<?> pullOffShelvesInfo(@RequestParam("id") String id) {
        return Result.OK("下架信息发布成功", super.service.pullOffShelvesInfo(id));
    }

    private static final String APPROVED_REVIEW = "信息发布-审核通过";
    @AutoLog(value = APPROVED_REVIEW)
    @ApiOperation(value = APPROVED_REVIEW, notes = APPROVED_REVIEW)
    @PutMapping(value = "/approvedReview")
    @RequiresPermissions("information:review")
    public Result<?> approvedReview(@RequestParam String id) {
        return Result.OK("审核已通过", super.service.approvedReview(id));
    }

    private static final String NOT_APPROVED_REVIEW = "信息发布-审核未通过";
    @AutoLog(value = NOT_APPROVED_REVIEW)
    @ApiOperation(value = NOT_APPROVED_REVIEW, notes = NOT_APPROVED_REVIEW)
    @PutMapping(value = "/notApprovedReview")
    @RequiresPermissions("information:review")
    public Result<?> notApprovedReview(@RequestParam String id) {
        return Result.OK("审核未通过", super.service.notApprovedReview(id));
    }

    private static final String DELETE = "信息发布-通过id删除";
    @AutoLog(value = DELETE)
    @ApiOperation(value = DELETE, notes = DELETE)
    @DeleteMapping(value = "/delete")
    @RequiresPermissions("information:delete")
    public Result<?> delete(@RequestParam("id") String id) {
        return Result.OK(DELETE_SUCCESS, super.service.deleteInformation(id));
    }

    private static final String DELETE_BATCH = "信息发布-批量删除";
    @AutoLog(value = DELETE_BATCH)
    @ApiOperation(value = DELETE_BATCH, notes = DELETE_BATCH)
    @DeleteMapping(value = "/deleteBatch")
    @RequiresPermissions("information:delete")
    public Result<?> deleteBatch(@RequestParam("ids") String ids) {
        return Result.OK(DELETE_SUCCESS, super.service.deleteBatchInformation(Arrays.asList(ids.split(","))));
    }

}
